package com.cqie.rbac.controller;

import com.cqie.rbac.entity.SysUser;
import com.cqie.rbac.service.ISysUserService;
import com.cqie.rbac.util.Result;
import com.cqie.rbac.bo.UserBo;
import com.cqie.rbac.bo.UserUpOrAddBo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 用户实体 前端控制器
 * </p>
 *
 * @author 夏兵
 * @since 2021-09-02
 */
@Api(tags = "用户管理")
@RestController
@RequestMapping("/user")
public class SysUserController {

    @Autowired
    private ISysUserService iSysUserService;

    @PostMapping("/login")
    @ApiOperation("登录")
    public Result login(@RequestBody SysUser sysUser) {
        return iSysUserService.login(sysUser.getAccount(), sysUser.getPassword());
    }

    @RequiresPermissions(value = {"all_user", "add_user", "del_user", "modify_user", "lock_user"}, logical = Logical.OR)
    @PostMapping("/pageOfUser")
    public Result<SysUser> pageOfUser(Integer current, Integer size, @RequestBody UserBo userBO) {
        return iSysUserService.pageOfUser(current, size, userBO);
    }

    @RequiresPermissions(value = {"all_user", "lock_user"}, logical = Logical.OR)
    @PostMapping("/changeUserStatus")
    public Result changeUserStatus(@RequestBody List<UserBo> userBos) {
        return iSysUserService.changeUserStatus(userBos);
    }

    @RequiresPermissions(value = {"all_user", "add_user", "modify_user"}, logical = Logical.OR)
    @PostMapping("/userUpOrAdd")
    public Result userUpOrAdd(@RequestBody UserUpOrAddBo userBo) {
        return iSysUserService.userUpOrAdd(userBo);
    }

    @ApiOperation("获取登录用户信息")
    @RequiresPermissions(value = {"all_user", "add_user", "modify_user"}, logical = Logical.OR)
    @GetMapping("/queryUserById")
    public Result queryUserById(Long id) {
        return iSysUserService.queryUserById(id);
    }

    @RequiresPermissions(value = {"all_user", "del_user"}, logical = Logical.OR)
    @PostMapping("/multiDelUser")
    public Result multiDelUser(@RequestBody int[] idArray) {
        return iSysUserService.multiDelUser(idArray);
    }

    @RequiresPermissions(value = {"all_user", "lock_user"}, logical = Logical.OR)
    @PostMapping("/multiChangeUsrStatus")
    public Result multiChangeUsrStatus(@RequestBody List<SysUser> idAndStatus) {
        return iSysUserService.multiChangeUsrStatus(idAndStatus);
    }
}
